﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace wspol_zad5
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void exitButton_Click(object sender, EventArgs e)
        {
            if (writersThreads != null)
            {
                foreach (Thread t in writersThreads)
                {
                    t.Abort();
                }
            }

            if (readersThreads != null)
            {
                foreach (Thread t in readersThreads)
                {
                    t.Abort();
                }
            }


            Application.Exit();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (writersThreads != null)
            {
                foreach (Thread t in writersThreads)
                {
                    t.Abort();
                }
            }

            if (readersThreads != null)
            {
                foreach (Thread t in readersThreads)
                {
                    t.Abort();
                }
            }


            Application.Exit();
        }

        private List<Thread> writersThreads;
        private List<Thread> readersThreads;

        private void runButton_Click(object sender, EventArgs e)
        {
            int libSize = (int)libNum.Value;
            int writersCnt = (int)writersNum.Value;
            int readersCnt = (int)readersNum.Value;

            if (isBookCB.Checked) libSize = 1;

            Resource res = new Resource(libSize, isBookCB.Checked);
            ThreadMenager men = new ThreadMenager(res);

            readersPB.Maximum = readersCnt;
            writersPB.Maximum = writersCnt;

            men.ChangeReadersCount = changeReadersProgBar;
            men.ChangeWritersCount = changeWritersProgBar;

            if (writersThreads != null)
            {
                for (int i = 0; i < writersThreads.Count; i++)
                {
                    writersThreads[i].Abort();
                }
            }
            if (readersThreads != null)
            {
                for (int i = 0; i < readersThreads.Count; i++)
                {
                    readersThreads[i].Abort();
                }
            }            

            writersThreads = new List<Thread>();
            readersThreads = new List<Thread>();

            for (int i = 0; i < writersCnt; i++)
            { 
                Thread w1 = new Thread(new ThreadStart(men.Writer));
                writersThreads.Add(w1);
                w1.Start();
            }

            for (int i = 0; i < readersCnt; i++)
            {
                Thread r1 = new Thread(new ThreadStart(men.Reader));
                readersThreads.Add(r1);
                r1.Start();
            }
            
        }

        private void changeReadersProgBar(int val)
        {
            readersPB.Invoke((MethodInvoker)delegate { readersPB.Value = val; });
            //readersPB.Value = val;
        }

        private void changeWritersProgBar(int val)
        {
            writersPB.Invoke((MethodInvoker)delegate { writersPB.Value = val; });
            //writersPB.Value = val;
        }

        private void isBookCB_CheckedChanged(object sender, EventArgs e)
        {
            if (isBookCB.Checked)
            {
                libNum.Enabled = true;
            }
            else
            {
                libNum.Enabled = false;
            }
        }

        
    }
}
